<?php
namespace app\index\controller;

use think\Db;

class Index extends Base
{
	public function index()
	{
        // 栏目ID
        $this->assign('column_id');
        
		return $this->fetch($this->tpl_path . 'index');
	}

    public function column()
    {
        $id = $this->request->param('id/d', 0);
        if (!array_key_exists($id, cache('column'))) {
            $this->error('栏目不存在');
        }

        // 栏目赋值
        $column = cache('column')[$id];
        $this->assign('column', $column);

        // 链接处理
        if (2 == $column['type']) {
            $this->redirect($column['url']);
        }

        // 单页处理
        $content = '';
        if (1 == $column['type']) {
            $content = Db::table('content_data')->where('column_id', $id)->value('content') ?: '';
        }
        $this->assign('content', $content);

        // 栏目ID
        $this->assign('column_id', $id);

        return $this->fetch($this->tpl_path . $column['tpl_column']);
    }

    public function content()
    {
        $content = Db::table('content')->leftJoin('content_data', 'content.id=content_data.content_id')->field('content.*,content_data.content')->where('content.id', $this->request->param('id/d', 0))->find();
        if (!$content) {
            $this->error('内容不存在');
        }
        $this->assign('content', $content);

        if (!array_key_exists($content['column_id'], cache('column'))) {
            $this->error('栏目不存在');
        }
        $column = cache('column')[$content['column_id']];
        $this->assign('column', $column);
        
        // 栏目ID
        $this->assign('column_id', $column['id']);        
        
        // 更新文章点击率
        Db::table('content')->where('id', $content['id'])->setInc('clicks');

        return $this->fetch($this->tpl_path . $column['tpl_content']);
    }

    public function test()
    {
        // return $this->fetch('pc/test');
    }
}
